<?php $this->pageHeader('Kalender'); ?>
<?php
    $nextMonth = new RedCross_Date($this->calendar->getDate());
    $nextMonth->add(1, Zend_Date::MONTH);


    $prevMonth = new RedCross_Date($this->calendar->getDate());
    $prevMonth->sub(1, Zend_Date::MONTH);
?>

<?php echo $this->flashMessage()->renderMessages(); ?>
<?php echo $this->flashMessage; ?>

<table id="calendar">
    <thead>
        <tr>
            <td class="previous">
                <a href='<?php echo $this->url(array('date' => $prevMonth->get(Admin_CalendarController::PARAMETER_DATE_FORMAT))) ?>'>
                    &lt;&lt; <?php echo ucfirst($prevMonth->get(Zend_Date::MONTH_NAME)) ?>
                </a>
            </td>
            <td class="month" colspan="5" align="center">
                <strong><?php echo ucfirst($this->calendar->getDate()->get(Zend_Date::MONTH_NAME))?></strong>
            </td>
            <td class="next">
                <a href='<?php echo $this->url(array('date' => $nextMonth->get(Admin_CalendarController::PARAMETER_DATE_FORMAT))) ?>'>
                    <?php echo ucfirst($nextMonth->get(Zend_Date::MONTH_NAME)) ?> &gt;&gt;
                </a>
            </td>
        </tr>
        <tr>
            <td class="day_name">Man</td>
            <td class="day_name">Tir</td>
            <td class="day_name">Ons</td>
            <td class="day_name">Tor</td>
            <td class="day_name">Fre</td>
            <td class="day_name">Lør</td>
            <td class="day_name">Søn</td>
        </tr>
    </thead>
    <tbody>
        <tr>
        <?php
            $numberOfBlankCells = $this->calendar->getNumberOfDaysInFirstWeekNotInThisMonth();

            $d = '';

            //Fill blank cells at the start of the calendar
            for ($i = 0; $i < $numberOfBlankCells; $i++) $d .= '<td class="blank">&nbsp;</td>';

            //Fill days in month
            while ($this->calendar->hasMoreDays()) {

                if ($this->calendar->isNewWeek()) {
                    $d .= '</tr><tr>';
                }

                $shifts = $this->calendar->getShiftsOnDay();
                $class = '';

                if ($shifts[0]) {
                    //$backgroundColor = '#a7ce92'; //'green';
                    $class = 'confirmed_shift';
                    $confirmedShift = true;
                    foreach ($shifts as $shift) {
                        $shiftId = $shift->id;
                        $shiftRoles = $shift->getMemberRoles($this->authInfo()->getMemberId());
                        foreach ($shiftRoles as $shiftRole) {
                            $temp = new Core_Model_ShiftRole();
                            unset($shiftRole->Member);
                            unset($shiftRole->Shift);
                            unset($shiftRole->Role);
                            $temp->fromArray($shiftRole->toArray());

                            if ($temp->getState() == Core_Model_ShiftRole::STATE_NOTIFICATION_RECEIVED) {
                                $cellText = '<a title="Vis vakt" alt="Vis vakt" href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shiftId), null, true) . '">' . $this->calendar->getCurrentDayNumber() . '</a>';
                                //$backgroundColor = '#e69a81'; //'redish';
                                $class = 'unconfirmed_shift';

                                $passiveMessage = $this->calendar->getPassiveMessageOnDay();
                                if ($passiveMessage) {
                                    $cellText .= ' <a title="Fjern passivmelding" href="' . $this->url(array('action' => 'remove-message', 'id' => $passiveMessage->id)) . '">[X]</a>';
                                }

                                $confirmedShift = false;
                                break;
                            }
                        }
                        if ($confirmedShift) {
                            $cellText = '<a title="Vis vakt" alt="Vis vakt" href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shiftId), null, true) . '">' . $this->calendar->getCurrentDayNumber() . '</a>';

                            $passiveMessage = $this->calendar->getPassiveMessageOnDay();
                            if ($passiveMessage) {
                                $cellText .= ' <a title="Fjern passivmelding" href="' . $this->url(array('action' => 'remove-message', 'id' => $passiveMessage->id)) . '">[X]</a>';
                            }

                            break;
                        }
                    }
                } else {
                    $cellText = $this->calendar->getCurrentDayNumber();
                    $passiveMessage = $this->calendar->getPassiveMessageOnDay();
                    if ($passiveMessage) {
                        //$backgroundColor = '#8dc3ca';
                        $class = 'passive';
                        $cellText .= '<a title="Fjern passivmelding" href="' . $this->url(array('action' => 'remove-message', 'id' => $passiveMessage->id)) . '">[X]</a>';
                    } else {
                        $inThreeWeeks = new RedCross_Date();
                        $inThreeWeeks->add(3, Zend_Date::WEEK);
                        $currentDay = $this->calendar->getCurrentDate();
                        if($currentDay->compare($inThreeWeeks) != -1) {
                            $cellText = '<a class="passiv_calendar_link" title="Meld passiv" href="' . $this->url(array('module' => 'core', 'controller' => 'calendar', 'action' => 'addpassiveday', 'date' => $this->calendar->getCurrentDate()->export()), null, true) . '">' . $cellText .'</a>';
                        }
                    }
                }
                $d .= '<td class="day ' . $class . '">';
                $d .= $cellText;
                $d .= '</td>';
                $this->calendar->dayPassed();
            }

            //Fill remaning cells of the last row
            while (!$this->calendar->isNewWeek()) {
                $d .= '<td>&nbsp;</td>';
                $this->calendar->dayPassed();
            }
        ?>
        <?php echo $d ?>
      </tr>
    </tbody>
</table>
<br />
<table id="calendar_color_codes" class="colorcodes clean">
  <tr>
        <td class="color"><img src="<?php echo $this->imageLink('images/icons/color_red.png'); ?>" alt="Ubekreftet vakt" title="Ubekreftet vakt"  /></td>
        <td class="description">Ubekreftet vakt.</td>
        <td class="color"><img src="<?php echo $this->imageLink('/images/icons/color_green.png'); ?>" alt="Bekreftet vakt" title="Bekreftet vakt" /></td>
        <td class="description">Bekreftet vakt.</td>
        <td class="color"><img src="<?php echo $this->imageLink('/images/icons/color_blue.png'); ?>" alt="Meldt passiv" title="Meldt passiv" /></td>
        <td class="description">Meldt passiv.</td>
    </tr>
</table>
<br />

</form>
